/*
 * XbLiteReader.h
 *
 *  Created on: Dec 19, 2009
 *      Author: evaleto
 */

#ifndef XBLITEREADER_H_
#define XBLITEREADER_H_

#include <WProgram.h>
#include <inttypes.h>

/**
 * Api Id constants (not everything is implemented!)
 */
#define TX_64_REQUEST 0x0
#define TX_16_REQUEST 0x1
#define AT_COMMAND_REQUEST 0x08
#define AT_COMMAND_QUEUE_REQUEST 0x09
#define REMOTE_AT_REQUEST 0x17
#define ZB_TX_REQUEST 0x10
#define ZB_EXPLICIT_TX_REQUEST 0x11
#define RX_64_RESPONSE 0x80
#define RX_16_RESPONSE 0x81
#define RX_64_IO_RESPONSE 0x82
#define RX_16_IO_RESPONSE 0x83
#define AT_RESPONSE 0x88
#define TX_STATUS_RESPONSE 0x89
#define MODEM_STATUS_RESPONSE 0x8a
#define ZB_RX_RESPONSE 0x90
#define ZB_EXPLICIT_RX_RESPONSE 0x91
#define ZB_TX_STATUS_RESPONSE 0x8b
#define ZB_IO_SAMPLE_RESPONSE 0x92
#define ZB_IO_NODE_IDENTIFIER_RESPONSE 0x95
#define AT_COMMAND_RESPONSE 0x88
#define REMOTE_AT_COMMAND_RESPONSE 0x97



#define 	START_IOPACKET    0x7e
#define 	SERIES1_IOPACKET  0x83

//ERROR
#define		OK				  0
#define 	BAD_ALLOC 		  1
#define 	UNEXPECTED_APPID  2
#define 	UNEXPECTED_START_IOPACKET 3
#define		NULL_STATE		  4
#define		NO_DATA		      5


class XbLiteReader {
public:
	XbLiteReader();
	~XbLiteReader();
	void setSerial(HardwareSerial &serial);
	void begin(long baud);
	void read();

	uint16_t app_id;
	uint16_t address_16;
	uint16_t local_checksum;
	uint8_t rssi;
	bool address_broadcast;
	bool pan_broadcast;

	uint8_t total_samples;
	uint8_t channel_indicator_high;
	uint8_t channel_indicator_low;

	uint8_t error;

private:
	void decode(uint8_t data[]);
        uint8_t readbyte();
	void clear();
	uint8_t* data;
	HardwareSerial* _serial;
};

#endif /* XBLITEREADER_H_ */
